﻿using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Interface;
using Medicalsystem.MCP.FormDesigner.Property;

namespace Medicalsystem.MCP.FormDesigner.Define
{
    public class XMLConvert
    {
        /*XML结构
         * 参见 XML结构.txt 说明文档
         *
         */

        /// <summary>
        /// XML 生成模板
        /// </summary>
        /// <param name="XML"></param>
        /// <param name="model"></param>
        public static void XMLConvertToModel(string XML, Model model)
        {
            if (XML.Length > 0)
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(XML);

                foreach (System.Xml.XmlNode XN in doc.DocumentElement.ChildNodes)
                {
                    switch (XN.Name.ToLower())
                    {
                        case "property":
                            ModelPropertyXML modelXML = new ModelPropertyXML(model.ModelForm);
                            modelXML.FromXML(XN.ChildNodes);
                            break;

                        case "controls":
                            foreach (System.Xml.XmlNode xn in XN.ChildNodes)
                            {
                                XMLConvertToControl(xn, model.ModelForm, model);
                            }
                            break;

                        case "comcontrols":
                            foreach (System.Xml.XmlNode xn in XN.ChildNodes)
                            {
                                XMLConvertToControl(xn, null, model);
                            }
                            break;

                        default:
                            break;
                    }
                }// end foreach
            }
        }

        private static void XMLConvertToControl(System.Xml.XmlNode XN, System.Windows.Forms.Control parentContainer, Model model)
        {
            System.Windows.Forms.Control ctrl = ControlBuilder.ControlNew(XN.FirstChild.ChildNodes[2].InnerText,XN.FirstChild.ChildNodes[0].InnerText, model, EDesignModeState.Run);

            if (ctrl != null && ctrl is ICtrlBase)
            {
                CtrlMng ctrlMng = new CtrlMng((ICtrlBase)ctrl);
                (ctrl as IXMLConvertor).FromXML(XN.ChildNodes[1].ChildNodes);  //此处必须要先属性赋值 再添加（涉及到控件名称检测）
                model.Controls.Ctrls.Add(ctrlMng);
                if (parentContainer != null)
                {
                    parentContainer.Controls.Add(ctrl);
                }

                if (ctrl is ICtrlContainer)
                {
                    System.Windows.Forms.Control container = null;

                    foreach (System.Xml.XmlNode cxn in XN.ChildNodes[2].ChildNodes)
                    {
                        container = (ctrl as ICtrlContainer).ControlContainers.Find(delegate(System.Windows.Forms.Control c) { return c.Name == cxn.ChildNodes[0].ChildNodes[2].InnerText; });
                        if (container != null)
                        {
                            XMLConvertToControl(cxn, container, model);
                        }
                    }
                }
            }
        }
    }
}